import React from 'react';
import { Spin } from 'antd';
import { isEqual, isEmpty } from 'lodash';
import {WeaErrorPage, WeaLocaleProvider, WeaTools} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
const { ls } = WeaTools;
import Title from './Title';
import { ContactsCom, SearchInput } from './Contacts';
import { setContentviewHeight } from '../../util/common';
//内容组件
class EContent extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            tabdata: props.edata.data,
            currentTab: props.edata.currenttab,
            refresh: false,
        }
        this.handleTabData = this.handleTabData.bind(this);
        this.searchContacts = this.searchContacts.bind(this);
    }
    componentWillReceiveProps(nextProps){
        if(!isEqual(nextProps, this.props)){
            this.setState({
                tabdata: nextProps.edata.data,
                currentTab: nextProps.edata.currenttab,
                refresh: false,
            }); 
        }
    }
    shouldComponentUpdate(nextProps, nextState){
        return !isEqual(this.state, nextState) || !isEqual(this.props, nextProps)
    }
    componentDidMount(){
        this.props.onRef(this);
    }
    handleTabData(tabid, value){
        const { params, isremembertab } = this.props.config;
        const { currentTab } = this.state;
        const tabdata_local = ls.getJSONObj('tabdata-' + params.eid + '-' + tabid);
        this.setState({
            refresh: tabid === currentTab,
            currentTab: tabid,
            tabdata: tabdata_local == null ? {} : tabdata_local,
        });
        WeaTools.callApi('/api/portal/element/contactstab', 'POST', {...params, tabid, key: typeof value == 'string'?value: ''}).then(result => {
            ls.set('tabdata-' + params.eid + '-' + tabid, result);
            if(tabid === this.state.currentTab){
                this.setState({
                    tabdata: result,
                    refresh: false,
                });
                if(isremembertab){
                    let edata = ls.getJSONObj('edata-' + params.eid);
                    if(edata){
                        edata.currenttab = tabid;
                        edata.data = result;
                        ls.set('edata-' + params.eid, edata);
                    }
                }
            }
       });
    }
    searchContacts(value){
        this.handleTabData(this.state.currentTab, value)
    }
    render() {
        const { config, edata, handleRefresh, handleDelete, fontSizeMp } = this.props;
        const { tabdata, currentTab, refresh } = this.state;
        const { tabids, titles, esetting } = edata;
        const { dataColoums=[] } = tabdata;
        let { fontStyle,header } = config.item;
        if(!isEmpty(fontSizeMp)) fontStyle = fontSizeMp;
        esetting.fontStyle = fontStyle;
        esetting.dataColoums = dataColoums;
        const { eid } = config.params;
        const { contentview = {} } = config.item;
        let cstyle = {};
        if(header.displayPosition && header.displayPosition == 'bottom' && tabids && tabids.length > 1){
            if(contentview.style.height){
                if(esetting.tabstyle == '1' && esetting.tabvalue){
                    cstyle = setContentviewHeight(contentview.style.height,esetting.tabvalue);
                    if(cstyle.height&&cstyle.height!=''){
                        cstyle={height: parseInt(cstyle.height)-33+"px",overflow: "auto"};
                    }     
                }else{
                    cstyle = {height: parseInt(contentview.style.height)-65+"px",overflow: "auto"};
                }   
            }else {
                cstyle = {height: contentview.style.height,overflow: "auto"};
            }
        }else if(tabids && tabids.length == 1){
            cstyle = {height: contentview.style.height,overflow: "auto"};
        }
        if(header.displayPosition && header.displayPosition == 'bottom') {
            if (tabids && tabids.length > 1 || tabids && tabids.length == 1 && esetting.isAlwaysShow == '1') {
                let elementH=parseInt(contentview.style.height)-parseInt($(`#titleContainer_${eid}`).css('height'))-33;
                if(elementH!=parseInt(cstyle.height)){
                    if(elementH<parseInt(cstyle.height)){
                        cstyle.height=elementH+"px";
                    }
                }
            }
        }

        let contentHtml = tabdata && tabdata.data && tabdata.data.length ? <ContactsCom ecId={`${this && this.props && this.props.ecId || ''}_ContactsCom@kq3tld`} eid={eid} list={tabdata.data} esetting={esetting}/> : <div/>;
        const _titleProps = {
            currentTab,
            config,
            titles,
            tabids,
            handleRefresh,
            handleDelete,
            fontSizeMp,
            tabwidth: esetting.tabwidth,
            tabvalue:esetting.tabvalue,
            tabname:esetting.tabname,
            tabstyle:esetting.tabstyle,
            methodvalue: esetting.methodvalue,
            handleTabData: this.handleTabData
        }
        return <div>
            <SearchInput ecId={`${this && this.props && this.props.ecId || ''}_SearchInput@u1sgbi`} placeholder={getLabel(83815,"搜索姓名/首字母/手机号")} type="contacts" onSearch={this.searchContacts} style={{width:'300px',marginTop:'5px',borderSpacing:'0'}}/>
            <Title ecId={`${this && this.props && this.props.ecId || ''}_Title@1vixug`} {..._titleProps}/>
            {/*<div className="tabContant" id={`tabcontant_${eid}`} style={cstyle}>*/}
            <div className="tabContant" id={`tabcontant_${eid}`} style={cstyle}>
                <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@bj9llc`} spinning={refresh}>{contentHtml}</Spin>
            </div>
         </div>;
    }
}

class MyErrorHandler extends React.Component {
    render() {
        const hasErrorMsg = this.props.error && this.props.error !== "";
            return ( <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@cly7j5`} msg = { hasErrorMsg ? this.props.error : getLabel(385218,"对不起，该页面异常，请联系管理员！")}/>
        );
    }
}
EContent = WeaTools.tryCatch(React, MyErrorHandler, { error: "" })(EContent);
export default EContent;
